using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using CommonLibrary.DBUtility;
using CommonLibrary.Model;

namespace CommonLibrary.DAL
{
    public class Linkman
    {
        public bool Exsits(int LinkmanID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@LinkmanID",SqlDbType.Int)
            };
            parameters[0].Value = LinkmanID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Linkman_Exsits", parameters);
                if (dr.Read())
                    return true;
                else
                    return false;
            }
        }
        public bool Insert(LinkmanInfo Linkman)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@LinkmanName",SqlDbType.VarChar,50),
                new SqlParameter("@LinkmanTel",SqlDbType.VarChar,50)
            };
            parameters[0].Value = Linkman.LinkmanName;
            parameters[1].Value = Linkman.LinkmanTel;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Linkman_Insert", parameters);
                return rowAffected > 0;
            }
        }
        public bool Update(LinkmanInfo Linkman)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@LinkmanID",SqlDbType.Int),
                new SqlParameter("@LinkmanName",SqlDbType.VarChar,50),
                new SqlParameter("@LinkmanTel",SqlDbType.VarChar,50)
            };
            parameters[0].Value = Linkman.LinkmanID;
            parameters[1].Value = Linkman.LinkmanName;
            parameters[2].Value = Linkman.LinkmanTel;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Linkman_Update", parameters);
                return rowAffected > 0;
            }
        }
        public bool Delete(int LinkmanID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@LinkmanID",SqlDbType.Int)
            };
            parameters[0].Value = LinkmanID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Linkman_Delete", parameters);
                return rowAffected > 0;
            }
        }
        public LinkmanInfo GetLinkman(int LinkmanID)
        {
            LinkmanInfo Linkman = null;
            SqlParameter[] parameters = {
                new SqlParameter("@LinkmanID",SqlDbType.Int)
            };
            parameters[0].Value = LinkmanID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Linkman_GetDetail", parameters);
                if (dr.Read())
                {
                    Linkman = new LinkmanInfo();
                    Linkman.LinkmanID = dr.GetInt32(0);
                    Linkman.LinkmanName = dr.GetString(1);
                    Linkman.LinkmanTel = dr.GetString(2);
                }
                return Linkman;
            }
        }
        public List<LinkmanInfo> GetList(string StrWhere)
        {
            List<LinkmanInfo> Linkmans = new List<LinkmanInfo>();
            LinkmanInfo Linkman = null;
            SqlParameter[] parameters = {
                new SqlParameter("@StrWhere",SqlDbType.NVarChar,4000)
            };
            parameters[0].Value = StrWhere;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Linkman_GetList", parameters);
                while (dr.Read())
                {
                    Linkman = new LinkmanInfo();
                    Linkman.LinkmanID = dr.GetInt32(0);
                    Linkman.LinkmanName = dr.GetString(1);
                    Linkman.LinkmanTel = dr.GetString(2);
                    Linkmans.Add(Linkman);
                }
                return Linkmans;
            }
        }
    }
}